package com.bayi.storage.base;

import android.app.Application;

import com.bayi.storage.R;
import com.bayi.storage.activity.CustomErrorActivity;
import com.bayi.storage.activity.MainActivity;
import com.bayi.storage.crash.CustomActivityOnCrash;
import com.bayi.storage.crash.config.CaocConfig;
import com.bayi.storage.model.StorageCabinetModel;
import com.bayi.storage.utils.LogUtil;
import com.bayi.storage.utils.SharedPreferenceUtil;
import com.lky.toucheffectsmodule.TouchEffectsManager;
import com.lky.toucheffectsmodule.types.TouchEffectsViewType;
import com.lky.toucheffectsmodule.types.TouchEffectsWholeType;

public class BaseApplication extends Application {

    static {
        TouchEffectsManager.build(TouchEffectsWholeType.SCALE)//设置全局使用哪种效果
                .addViewType(TouchEffectsViewType.ALL)//添加哪些View支持这个效果
                .setListWholeType(TouchEffectsWholeType.RIPPLE);//为父控件为列表的情况下，设置特定效果
    }

    private static final String TAG = BaseApplication.class.getSimpleName();

    private static BaseApplication app;

    public static BaseApplication getApp() {
        return app;
    }

    public StorageCabinetModel storageCabinetModel;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        caoConfig();
    }

    public StorageCabinetModel getStorageCabinetModel() {
        storageCabinetModel = (StorageCabinetModel) SharedPreferenceUtil.getSharedPreferenceUtil(this).getObjectBean(StorageCabinetModel.class, SharedPreferenceUtil.SP_LOCAL);
        if (storageCabinetModel == null) {
            storageCabinetModel = new StorageCabinetModel();
            SharedPreferenceUtil.getSharedPreferenceUtil(this).saveObjectBean(storageCabinetModel, SharedPreferenceUtil.SP_LOCAL);
        }
        return storageCabinetModel;
    }

    public synchronized void setStorageCabinetModel(StorageCabinetModel localModel) {
        SharedPreferenceUtil.getSharedPreferenceUtil(BaseApplication.getApp())
                .saveObjectBean(localModel, SharedPreferenceUtil.SP_LOCAL);
        this.storageCabinetModel = localModel;
    }


    private void caoConfig() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM)
                //This disables the interception of crashes. Use it to disable CustomActivityOnCrash (for example, depending on your buildType).
                .enabled(true)
                //This hides the "error details" button in the error activity, thus hiding the stack trace
                .showErrorDetails(false)
                //This avoids the app from using the "Restart app" button and displaying a "Close app" button directly.
                //Even with restart app enabled, the Close app can still be displayed if your app has no launch activity.
                .showRestartButton(false)
                //This makes the library track the activites visited by the user and their lifecycle calls.
                //Use it if you want that info in the error details screen shown on the error activity.
                .trackActivities(true)
                //This hides the additional log shown when the error activity is launched.
                //It is shown by default because the Android Studio Logcat view by default only shows
                //the current process output, and this makes the stack trace more obvious to find.
                .logErrorOnRestart(false)
                //Defines the time that must pass between app crashes to determine that we are not in a crash loop.
                //If a crash has occurred less that this time ago, the error activity will not be launched
                //and the system crash screen will be invoked.
                .minTimeBetweenCrashesMs(2000)
                //This shows a different image on the error activity, instead of the default upside-down bug.
                //You may use a drawable or a mipmap.
                .errorDrawable(R.mipmap.ic_launcher)
                //This sets the restart activity.
                //If you set this, this will be used. However, you can also set it with an intent-filter:
                //  <action android:name="cat.ereza.customactivityoncrash.RESTART" />
                //If none are set, the default launch activity will be used.
                .restartActivity(MainActivity.class)
                //This sets a custom error activity class instead of the default one.
                //If you set this, this will be used. However, you can also set it with an intent-filter:
                //  <action android:name="cat.ereza.customactivityoncrash.ERROR" />
                //If none are set, the default launch activity will be used.
                //Comment it (and disable the intent filter) to see the customization effects on the default error activity.
                //Uncomment to use the custom error activity
                .errorActivity(CustomErrorActivity.class)
                //This sets a EventListener to be notified of events regarding the error activity,
                //so you can, for example, report them to Google Analytics.
                .eventListener(new CustomEventListener())
                .apply();
        //如果没有任何配置，程序崩溃显示的是默认的设置
        CustomActivityOnCrash.install(this);
    }

    private static class CustomEventListener implements CustomActivityOnCrash.EventListener {
        @Override
        public void onLaunchErrorActivity() {
            LogUtil.v(TAG, "onLaunchErrorActivity()");
        }

        @Override
        public void onRestartAppFromErrorActivity() {
            LogUtil.v(TAG, "onRestartAppFromErrorActivity()");
        }

        @Override
        public void onCloseAppFromErrorActivity() {
            LogUtil.v(TAG, "onCloseAppFromErrorActivity()");
        }
    }

}